//////////////////////////////////////////////////////////////////////////
// FileName:LoadMenu
// CreateBy:DragonXiong
// CreateDate:2009-5-4
// Description:
//		Class LoadMenu
//      
// Modify Log:090602:Change Session Method,Change DataTable Filed to Model Method -Dragon
//            090612:Change Get System ID to SessionBox.CurrentSystem --dragon
//            091228:Set the SessionBox.CurrentSystem only the QueryString["SysID"] is not null or empty --Dragon
//            20100129:Fix the problem only get role function --DragonXiong
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using BLL=Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;
using Auto.Data.Security;

namespace BaseSystem
{
    public partial class LoadMenu : System.Web.UI.Page
    {
        BLL.SysFunctions funcs = new BLL.SysFunctions();
        protected void Page_Load(object sender, EventArgs e)
        {
            //  SessionBox.CurrentSystem = Request.QueryString["SysID"];
            if (!string.IsNullOrEmpty(Request.QueryString["SysID"]))
            {
                SessionBox.CurrentSystem = Request.QueryString["SysID"];//"_System";
            }
            if (!IsPostBack)
            {
                InitMenu(SessionBox.CurrentSystem);
            }
        }


        // Menu menu = new Menu();
        private void InitMenu(string sysID)
        {
            //DataTable dtMenu1 = funcs.GetSysFunctionsTop(SessionBox.CultrueInfo, sysID);// fmenu.GetMenu(SysID);

            //UsersRoles uroles = new UsersRoles();
            //DataTable originData = uroles.GetUserRolesFunctions(SessionBox.User.UserID, sysID, SessionBox.CultrueInfo);
            DataTable originData = SessionBox.User.GetUserFunctions(SessionBox.User.UserID, sysID, SessionBox.CultrueInfo);

            DataView dv = originData.DefaultView;
            dv.RowFilter = string.Format("{0} IS NULL or {0}='' AND IsMenu=1", Model.SysFunctions.FIELD_ParentID); 

            DataTable dtMenu = dv.ToTable(true, new string[] { Model.SysFunctions.FIELD_FuncID, Model.SysFunctions.FIELD_SysID, 
                                                               Model.SysFunctions.FIELD_URL, Model.SysFunctions.FIELD_Target, 
                                                               Model.SysFunctions.FIELD_FuncName });

            for (int i = 0; i < dtMenu.Rows.Count; i++)
            {
                HtmlAnchor a = new HtmlAnchor();
                a.HRef = dtMenu.Rows[i][Model.SysFunctions.FIELD_URL].ToString();
                a.Target = dtMenu.Rows[i][Model.SysFunctions.FIELD_Target].ToString();
                a.InnerHtml = "<span>" + dtMenu.Rows[i][Model.SysFunctions.FIELD_FuncName].ToString() + "</span>";

                HtmlGenericControl li = new HtmlGenericControl("li");
                li.Controls.Add(a);
                InitSubMenu(li, dtMenu.Rows[i][Model.SysFunctions.FIELD_FuncID].ToString(), originData);

            }
        }



        private void InitSubMenu(HtmlGenericControl hc, string parentID, DataTable dtSubMenu)
        {
            if (dtSubMenu.Rows.Count > 0)
            {
                //for (int i = 0; i < dtSubMenu.Rows.Count; i++)
                //{
                //    if (Convert.ToBoolean(dtSubMenu.Rows[i][Model.SysFunctions.FIELD_IsMenu]))
                //    {
                        DataTable originData = dtSubMenu.Copy();
                        DataRow[] rows = dtSubMenu.Select(string.Format("{0} ='{1}'", Model.SysFunctions.FIELD_ParentID, parentID));
                        HtmlGenericControl ul = new HtmlGenericControl("ul");
                        foreach (DataRow dataRow in rows)
                        {
                            HtmlAnchor a = new HtmlAnchor();
                            a.HRef = dataRow[Model.SysFunctions.FIELD_URL].ToString();
                            a.Target = dataRow[Model.SysFunctions.FIELD_Target].ToString();
                            a.InnerHtml = "<span>" + dataRow[Model.SysFunctions.FIELD_FuncName].ToString() + "</span>";

                            HtmlGenericControl li = new HtmlGenericControl("li");
                            li.Controls.Add(a);

                            //if (Convert.ToInt32(dataRow[Model.SysFunctions.FIELD_IsEnd]) == 1)
                            if(Convert.ToInt32(dataRow[Model.SysFunctions.FIELD_IsEnd]) == 1 || (Convert.ToInt32(dataRow[Model.SysFunctions.FIELD_IsMenu])==1 && dataRow[Model.SysFunctions.FIELD_URL].ToString() != "") )
                            {
                                ul.Controls.Add(li);
                            }
                            else
                            {
                                InitSubMenu(li, dataRow[Model.SysFunctions.FIELD_FuncID].ToString(), originData);
                                ul.Controls.Add(li);
                            }
                        }
                        hc.Controls.Add(ul);
                        qm0.Controls.Add(hc);
                //    }
                //}
            }
        }

    }
}
